<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h1 class="login-title">欢迎登录</h1>
        <p class="login-subtitle">请输入您的账号信息</p>
      </div>

      <el-form
          ref="loginFormRef"
          :model="loginForm"
          :rules="loginRules"
          label-position="top"
          @keyup.enter="handleLogin"
      >
        <el-form-item label="微信号" prop="wechatId">
          <el-input
              v-model="loginForm.wechatId"
              placeholder="请输入您的微信号"
              size="large"
              clearable
          >
            <template #prefix>
              <el-icon><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input
              v-model="loginForm.password"
              placeholder="请输入您的密码"
              type="password"
              size="large"
              show-password
              clearable
          >
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item>
          <el-button
              type="primary"
              class="login-btn"
              :loading="loading"
              size="large"
              @click="handleLogin"
          >
            登 录
          </el-button>
        </el-form-item>
      </el-form>

      <div class="login-footer">
        <p class="footer-text">还没有账号？<a href="/register" class="link">立即注册</a></p>
        <p class="footer-text"><a href="#" class="link">忘记密码？</a></p>
      </div>
    </div>

    <div class="login-bg"></div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import { User, Lock } from '@element-plus/icons-vue';
import axios from 'axios';

const router = useRouter();

// 登录表单数据
const loginForm = ref({
  wechatId: '',
  password: '',
});

// 表单验证规则
const loginRules = {
  wechatId: [
    { required: true, message: '请输入微信号', trigger: 'blur' },
    { min: 3, message: '微信号长度不能小于3个字符', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能小于6个字符', trigger: 'blur' },
  ],
};

const loading = ref(false);
const loginFormRef = ref(null);

// 登录处理
// 登录处理
const handleLogin = async () => {
  try {
    await loginFormRef.value.validate();
    loading.value = true;

    const params = new URLSearchParams();
    params.append('wechatId', loginForm.value.wechatId);
    params.append('password', loginForm.value.password);

    const response = await axios.post('http://localhost:8080/auth/login', params, {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      }
    });

    if (response.data.code === 200) {
      const token = response.data.result; // 根据你的API响应结构调整
      localStorage.setItem('access_token', token);
      localStorage.setItem('wechat_id', loginForm.value.wechatId)
      // 跳转到home页面并携带token
      await router.push({
        path: '/',
      });
    } else {
      ElMessage.error(response.data.message || '登录失败');
    }
  } catch (error) {
    if (error.response && error.response.data) {
      ElMessage.error(error.response.data.message || '登录失败');
    } else {
      ElMessage.error('请求失败，请检查网络连接');
    }
    console.error('登录错误:', error);
  } finally {
    loading.value = false;
  }
};
</script>

<style scoped>
.login-container {
  display: flex;
  min-height: 100vh;
  background-color: #f8fafc;
}

.login-bg {
  flex: 1;
  background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
  background-size: cover;
  display: none;
}

.login-box {
  width: 100%;
  max-width: 480px;
  padding: 2rem;
  margin: auto;
  background-color: #ffffff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  border-radius: 0.5rem;
}

.login-header {
  margin-bottom: 2rem;
  text-align: center;
}

.login-title {
  font-size: 1.75rem;
  font-weight: 600;
  color: #1e293b;
  margin-bottom: 0.5rem;
}

.login-subtitle {
  font-size: 1rem;
  color: #64748b;
}

.login-btn {
  width: 100%;
  margin-top: 1rem;
  height: 48px;
  font-size: 1rem;
}

.login-footer {
  margin-top: 2rem;
  text-align: center;
}

.footer-text {
  color: #64748b;
  font-size: 0.875rem;
  margin: 0.5rem 0;
}

.link {
  color: #4f46e5;
  text-decoration: none;
  transition: color 0.2s;
}

.link:hover {
  color: #7c3aed;
  text-decoration: underline;
}

/* 响应式设计 - 大屏幕 */
@media (min-width: 1024px) {
  .login-container {
    padding: 2rem;
  }

  .login-bg {
    display: block;
    border-radius: 1rem;
    margin-left: 2rem;
  }

  .login-box {
    padding: 3rem;
  }
}

/* 输入框样式优化 */
:deep(.el-input__wrapper) {
  padding: 0.5rem 1rem;
}

:deep(.el-input__prefix) {
  display: flex;
  align-items: center;
  margin-right: 0.5rem;
}

/* 表单标签样式优化 */
:deep(.el-form-item__label) {
  font-weight: 500;
  color: #475569;
  padding-bottom: 0.25rem;
}
</style>